package battleship_client.screens;

import battleship_client.Controller;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;

/**
 * GameScreen la ventana de juego
 * @author jalejandrovc
 */
public class GameScreen extends Screen {

    /**
     * Creates a new GameScreen object
     * @param wh a window handler
     */
    public GameScreen(Controller wh, int[] ships) {
        windowHandler = wh;
        setLookAndFeel();
        initComponents(ships);
    }

    /**
     * Initializes the GameScreen components
     */
    private void initComponents(int[] ships) {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setSize(900, 650);
        setLocationRelativeTo(null);
        setResizable(false);

        contentPane = getContentPane();

        add(layeredPane, BorderLayout.CENTER);
        layeredPane.setBounds(0, 0, 900, 650);
        contentPane.setPreferredSize(new Dimension(900, 650));
        layeredPane.setSize(900, 650);
        opponentImagePanel = new JPanel();
        ownImagePanel = new JPanel();
        setTxtMessage(new JTextArea("HOLAAAAA!!!!", 450, 115));
        getTxtMessage().setLineWrap(true);
        scrollablePane = new JScrollPane(getTxtMessage());
        scrollablePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        lblImageOpponent = new JLabel();
        lblImageOpponent.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/tablero.png")));
        lblImageOwn = new JLabel();
        setLblError(new JLabel("Error"));
        opponentImagePanel.add(lblImageOpponent);
        opponentImagePanel.setBounds(0, 0, 900, 535);
        opponentImagePanel.setOpaque(true);

        ownImagePanel.add(lblImageOwn);
        ownImagePanel.setBounds(42, 134, 300, 272);
        ownImagePanel.setOpaque(true);

        scrollablePane.setBounds(450, 535, 450, 115);
        scrollablePane.add(getTxtMessage());

        getLblError().setBounds(50, 580, 350, 30);
        getLblError().setFont(new Font("Lucida Grande", 1, 12));
        getLblError().setForeground(Color.red);
        getLblError().setVisible(false);

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                opponentPanel[i][j] = new JPanel();
                opponentPanel[i][j].setBounds(426 + (i * 42), 106 + (j * 38), 34, 30);
                opponentPanel[i][j].setOpaque(true);
                opponentPanel[i][j].setBackground(Color.blue);
                final String x = "" + (i+1);
                final String y = "" + (j+1);
                opponentPanel[i][j].addMouseListener(new java.awt.event.MouseListener() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        attack(x, y);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                });
                layeredPane.add(opponentPanel[i][j]);
            }
        }

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                getOwnPanel()[i][j] = new JPanel();
                getOwnPanel()[i][j].setBounds(70 + (i * 27), 168 + (j * 24), 24, 21);
                getOwnPanel()[i][j].setOpaque(true);
                getOwnPanel()[i][j].setBackground(Color.blue);
                layeredPane.add(getOwnPanel()[i][j]);
            }
        }

        for (int x = 2; x < 7; x++) {
            if ((ships[(x - 2) * 3] == 1 ? true : false)) {
                for (int i = 0; i <= x; i++) {
                    getOwnPanel()[ships[((x - 2) * 3) + 1] - 1][ships[((x - 2) * 3) + 2] + i - 1].setBackground(Color.GREEN);
                }
            } else {
                for (int i = 0; i < x; i++) {
                    getOwnPanel()[ships[((x - 2) * 3) + 1] + i - 1][ships[((x - 2) * 3) + 2] - 1].setBackground(Color.GREEN);
                }
            }
        }


        layeredPane.add(getLblError());
        
        layeredPane.add(scrollablePane);
        layeredPane.add(opponentImagePanel);
        layeredPane.add(ownImagePanel);
        contentPane.add(layeredPane);
        this.pack();
    }

    /**
     * Sends the attack input for validation
     * @param x the string inserted into the X text field
     * @param y the string inserted into the Y text field
     */
    private void attack(String x, String y) {
        if (attack){
            windowHandler.validator.validateMove(this, x, y);
        }
    }
    private boolean attack = false;
    private Controller windowHandler;
    private JLayeredPane layeredPane = new JLayeredPane();
    private JPanel opponentPanel[][] = new JPanel[10][10];
    private JPanel ownPanel[][] = new JPanel[10][10];
    
    private Container contentPane;
    private JPanel ownImagePanel, opponentImagePanel;
    private JLabel lblImageOpponent, lblImageOwn, lblError;
    private JScrollPane scrollablePane;
    private JTextArea txtMessage;

    /**
     * @return the boardPanel
     */
    public JPanel[][] getOpponentPanel() {
        return opponentPanel;
    }

    /**
     * @return the ownPanel
     */
    public JPanel[][] getOwnPanel() {
        return ownPanel;
    }

    /**
     * @param ownPanel the ownPanel to set
     */
    public void setOwnPanel(JPanel[][] ownPanel) {
        this.ownPanel = ownPanel;
    }

    /**
     * @return the txtMessage
     */
    public JTextArea getTxtMessage() {
        return txtMessage;
    }

    /**
     * @param txtMessage the txtMessage to set
     */
    public void setTxtMessage(JTextArea txtMessage) {
        this.txtMessage = txtMessage;
    }

    /**
     * @return the lblError
     */
    public JLabel getLblError() {
        return lblError;
    }

    /**
     * @param lblError the lblError to set
     */
    public void setLblError(JLabel lblError) {
        this.lblError = lblError;
    }

    /**
     * @return the attack
     */
    public boolean isAttack() {
        return attack;
    }

    /**
     * @param attack the attack to set
     */
    public void setAttack(boolean attack) {
        this.attack = attack;
    }
}
